package com.jz.kufangkuqu.biaoshi.service.impl;

import com.jz.beijianbao.beijian.personEnum.PersonEnum;
import com.jz.kufangkuqu.biaoshi.bean.*;
import com.jz.kufangkuqu.biaoshi.mapper.BiaoShiMapper;
import com.jz.kufangkuqu.biaoshi.service.BiaoShiService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BiaoShiServiceImpl implements BiaoShiService {

    @Autowired
    private BiaoShiMapper biaoShiMapper;

    @Override
    public ResultVO queryZhuangTai() {
        List<BiaoShi> biaoShiList = biaoShiMapper.queryZhuangTai();
        for (BiaoShi plant: biaoShiList) {
            Integer enabled = plant.getStatus() ? 1:0;
            plant.setStatusName(PersonEnum.getName(enabled));
        }
        return ResultBuildVO.success(biaoShiList);
    }

    @Override
    public ResultVO queryPage(BiaoShiSelect biaoShiSelectVo) {
        List<BiaoShi> biaoShiList = biaoShiMapper.queryPage(biaoShiSelectVo);
        for (BiaoShi plant: biaoShiList) {
            Integer enabled = plant.getStatus() ? 1:0;
            plant.setStatusName(PersonEnum.getName(enabled));
        }
        int count = biaoShiMapper.count(biaoShiSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(biaoShiSelectVo.getPageIndex());
        allRecordsVo.setPageSize(biaoShiSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(biaoShiList);
        return ResultBuildVO.success(allRecordsVo);
    }

    @Override
    public ResultVO add(BiaoShiAdd biaoShiAdd) {
        Date now = new Date();
        BiaoShi legalPlantVo1 = new BiaoShi();
        BeanUtils.copyProperties(biaoShiAdd,legalPlantVo1);
        legalPlantVo1.setCreateName("从session中获取");
        legalPlantVo1.setCreateTime(now);
        biaoShiMapper.add(legalPlantVo1);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO update(BiaoShiUpdate biaoShiUpdate) {
        Date now = new Date();
        BiaoShi legalPlant1 = new BiaoShi();
        BeanUtils.copyProperties(biaoShiUpdate,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        int version = biaoShiMapper.updatePlant(legalPlant1);
        if (version==0){
            return ResultBuildVO.error("情刷新后修改");
        }
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateStatus(BiaoShiUpdatestatus biaoShiUpdatestatus) {
        Date now = new Date();
        BiaoShi legalPlant1 = new BiaoShi();
        BeanUtils.copyProperties(biaoShiUpdatestatus,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        biaoShiMapper.updateStatus(legalPlant1);
        return ResultBuildVO.success();
    }



}
